package com.zzh.htts.voice;


import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.zzh.htts.voice.databinding.ActPcmListBinding;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by zzh on 2024/12/16.
 *
 * @date: 2024/12/16 13:59
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.htts.voice
 */
public class PcmListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    ActPcmListBinding bind;
    Adapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bind = ActPcmListBinding.inflate(getLayoutInflater(), null, false);
        setContentView(bind.getRoot());
        adapter = new Adapter(this);
        Log.e("-----cache", "cache: " + getCacheDir().getAbsolutePath());
        File[] list = getCacheDir().listFiles((dir, name) -> name.endsWith(".wav"));
        adapter.addAll(list);
        bind.lvList.setAdapter(adapter);
        bind.lvList.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        File item = adapter.getItem(position);
        if (item != null) {
            play(item);
        }
    }

    private static final int BUFFER_SIZE = 14144;

    private void play(File item) {
        Log.d("MainActivity", "lu yin kaishi");
        int streamType = AudioManager.STREAM_MUSIC;
        int simpleRate = 16000;
        int channelConfig = AudioFormat.CHANNEL_OUT_MONO;
        int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
        int mode = AudioTrack.MODE_STREAM;

        int minBufferSize = AudioTrack.getMinBufferSize(simpleRate, channelConfig, audioFormat);

        AudioTrack audioTrack = new AudioTrack(streamType, simpleRate, channelConfig, audioFormat,

                Math.max(minBufferSize, BUFFER_SIZE), mode);
        int sampleRate = audioTrack.getSampleRate();

        audioTrack.play();
        Log.d("pcm1", minBufferSize + " is the min buffer size , " + BUFFER_SIZE + " is the read buffer size");
        Log.d("pcm2", sampleRate + " sampleRate");
        byte[] mBuffer = new byte[minBufferSize];
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(item);
            int read;
            while ((read = inputStream.read(mBuffer)) > 0) {
                audioTrack.write(mBuffer, 0, read);
            }
            audioTrack.stop();
            audioTrack.release();
        } catch (RuntimeException | IOException e) {
            e.printStackTrace();
        }
    }

    class Adapter extends ArrayAdapter<File> {

        public Adapter(@NonNull Context context) {
            super(context, R.layout.item_name);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_name, null, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            File item = getItem(position);

            holder.tvPath.setText(item.getAbsolutePath());
            holder.tvName.setText(item.getName());

            return convertView;
        }

        class ViewHolder {
            TextView tvName, tvPath;

            public ViewHolder(View itemView) {
                tvName = itemView.findViewById(R.id.tv_name);
                tvPath = itemView.findViewById(R.id.tv_path);
            }
        }

    }
}
